//
//  ViewController.swift
//  myMoya
//
//  Created by xiaoyi on 2020/4/26.
//  Copyright © 2020 lete. All rights reserved.
//

import UIKit
import ObjectMapper

class ViewController: UIViewController {
    
    lazy var myBtn: UIButton = {
        //创建按钮
        let jumpBtn  = UIButton(type: UIButton.ButtonType.custom)
                jumpBtn.setTitle("网络请求", for: .normal)
                jumpBtn.frame = CGRect(x: 100, y: 150, width: 100, height: 100)
                jumpBtn.backgroundColor = UIColor(red: 50/255, green: 123/255, blue: 203/255, alpha: 1)
                jumpBtn.setTitleColor(UIColor.white, for: .normal)
                //按钮绑定事件，点击时执行
                jumpBtn.addTarget(self, action:  #selector(pageJump), for: .touchDown)
        return jumpBtn
    }()
    
    
    lazy var myimageView: UIImageView = {
        var imgview = UIImageView()
        imgview.backgroundColor = UIColor.gray
        imgview.frame = CGRect(x: 220, y: 100, width: 100, height: 100)
        return imgview
    }()
    
    
    lazy var englishBtn: UIButton = {
           //创建按钮
           let btn  = UIButton(type: UIButton.ButtonType.custom)
                   btn.setTitle("英文", for: .normal)
                   btn.frame = CGRect(x: 10, y: 250, width: 100, height: 30)
        btn.backgroundColor = UIColor.systemBlue
                   btn.setTitleColor(UIColor.white, for: .normal)
                   //按钮绑定事件，点击时执行
                   btn.addTarget(self, action:  #selector(englishBtnHanlde), for: .touchDown)
           return btn
       }()
    lazy var chineseBtn: UIButton = {
              //创建按钮
              let btn  = UIButton(type: UIButton.ButtonType.custom)
                      btn.setTitle("中文", for: .normal)
                      btn.frame = CGRect(x:  200, y: 250, width: 100, height: 30)
           btn.backgroundColor = UIColor.systemBlue
                      btn.setTitleColor(UIColor.white, for: .normal)
                      //按钮绑定事件，点击时执行
                      btn.addTarget(self, action:  #selector(chineseBtnHandle), for: .touchDown)
              return btn
          }()
    
    lazy var textLabel: UILabel = {
        let label = UILabel(frame: CGRect(x: 100, y: 300, width: 190, height: 40))
        //设置label的文本
        label.text = "语言"
        label.textColor = UIColor.white//设置文字的颜色
        label.backgroundColor = UIColor.blue//设置背景颜色
        return label
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        
        setupUI()
       
        strHandle()
        
        view.addSubview(myBtn)
        
        
    }
    
    func setupUI() {
        self.view.backgroundColor = UIColor.random
        
        view.addSubview(myimageView)
        view.addSubview(englishBtn)
        view.addSubview(chineseBtn)
        view.addSubview(textLabel)
    }
    
    func strHandle() {
         
               let str1 = "欢迎来到zhuhai"
               let md55 = str1.md5
               LTLog("md55 = \(md55  ?? "")")
               
               let base64Str = "让天方夜谭天"
               let encodeStr = base64Str.encodeToBase64()
               let decodeStr = encodeStr.decodeBase64()
               LTLog(encodeStr)
               LTLog(decodeStr)
               
               
               let nonce = String.randomStr(len: 6)
               LTLog("6 位随机字符串: \(nonce)")
               
               LTLog( "当前时间戳: \(LTDateTool.getStamp())")
               
    }
    
    
    func getNetDataMethod() {
        
        //MARK: - 网络请求
        LTNetwork.request(target: .goods(route: "app/getData", version: 1), success: { (result) in
            //            LTLog(result)
            
            //得到基础的结构数据
            guard let baseModel = NetBaseModel(JSON: (result as! [String : AnyObject])) else { return }
            LTLog("数据 msg: \(baseModel.status ?? 0)")
            LTLog("数据 msg: \(baseModel.text)")
            LTLog("数据 msg: \(baseModel.textstr)")
            //            LTLog("数据 msg: \(String(describing: baseModel.data))")
            
            
            //使用Mapper
            //把对象转成 model
            guard let shopModel = Mapper<ShopModel>().map(JSONObject: baseModel.data)  else{ return}
            LTLog(shopModel.serverTime)
            LTLog(shopModel.shop_config)
            LTLog("config======>> \(shopModel.shop_config?.keywords ?? "")")
            LTLog("banner======>> \(shopModel.banner)")
            
            //使用Mapper转模型数组
            //            //使用Mapper转模型数组
            //let users: [User] = Mapper<User>().mapArray(JSONString: JSONString)
            
            //从 model 中直接取值
            let bannerModelArr: [ShopBannerModel] = shopModel.banner
            LTLog(bannerModelArr)
            
            LTLog("banner 名字=====>>> \(String(describing: bannerModelArr[0].name))")
            
            let sidebarArr: [ShopSidebarModel] = shopModel.sidebar
            
           
            
            //菊花指示器消失
              CLMPHUD.dissmissHUD(self.view)
            
            CLMPHUD.showHUDText(self.view, "\(shopModel.shop_config?.description ?? "")", 2.0)
            
//            图片加载
            // 带背景图片
                       let image = UIImage(named: "rrr")
                       self.myimageView.kf.setImage(with: URL(string: bannerModelArr[0].image), placeholder: image, options: nil, progressBlock: nil, completionHandler: nil)
           
            
        }) { (error) in
            LTLog(error.localizedDescription)
            //菊花指示器消失
            CLMPHUD.dissmissHUD(self.view)
        }
    }
    
    
    
    //MARK: -
    //实现按钮方法
    @objc func pageJump() {
        //菊花指示器出现
        CLMPHUD.showHUD(view: view)
         getNetDataMethod()
       }
    
    @objc func englishBtnHanlde() {
         AppSettings.shared.language = .English //  .Chinese
        
        textLabel.text = String.localKeyStr(key: "ddd", comment: "")
    }
    
    @objc func chineseBtnHandle() {
         AppSettings.shared.language = .Chinese //  .Chinese
         textLabel.text = String.localKeyStr(key: "ddd", comment: "")
    }
}

